import React from 'react';
import { Link } from 'react-router-dom';
import { Button } from 'antd';
import http from '../../assets/js/http'
import Sub1 from './sub/sub1'
import Sub2 from './sub/sub2'
import {cityData} from './sub/city'

class Home extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            count: 0,
            num: 0,
            brotherData: ''
        }
    }
    // 加按钮 点击事件
    addBtnClick(){
        let count = this.state.count;
        count ++;
        this.setState({
            count: count
        })
    }
    // 子组件1事件回调
    reduceBtnClickCallBack(num){
        this.setState({
            num: num
        })
    }
    // 子组件2事件回调
    brotherBtnClickCallBack(num){
        this.setState({
            brotherData: num
        })
    }
    // 获取IP
    async getMyIP(){
        let baseUrl = "https://api.apiopen.top/EmailSearch";
        let params = {
            number: '1012002'
        }
        var data = await http.get(baseUrl,params);
    }


    makeData(){
        let tempData = cityData;
        let len1 = tempData.length;
        let tempCity = [];
        for (let i = 0; i < len1; i++) {
            tempData[i].value = String(tempData[i].value);
            if (tempData[i].children) {
                for (let j = 0; j < tempData[i].children.length; j++) {
                    tempData[i].children[j].value = String(tempData[i].children[j].value)
                    if (tempData[i].children[j].children) {
                        for (let k = 0; k < tempData[i].children[j].children.length; k++) {
                            tempData[i].children[j].children[k].value = String(tempData[i].children[j].children[k].value)
                        }
                    }
                }
            }
        }
        console.log('tempData',JSON.stringify(tempData));
    }


    render() {
        return (
            <div className='home'>
                <Link to="/detail">Home</Link>
                我是首页
                <div>count的值: {this.state.count}</div>
                <Button onClick={this.addBtnClick.bind(this)} type="primary">加按钮</Button>
                <div>接收到子组件的值: {this.state.num}</div>
                <Sub1 
                    reduceBtnClickCallBack={this.reduceBtnClickCallBack.bind(this)}
                    brotherData={this.state.brotherData}
                    count={this.state.count}>
                </Sub1>
                <Sub2
                    brotherBtnClickCallBack={this.brotherBtnClickCallBack.bind(this)}>
                </Sub2>
            </div>
        );
    }
    componentDidMount() {
        this.getMyIP();
        this.makeData();
    }
}

export default Home;
